package view.components.table;

import java.awt.Component;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import view.components.IconizedInterface;

@SuppressWarnings("serial")
public class IconizedCellRenderer extends DefaultTableCellRenderer {
	private static DateFormat dateFormat = DateFormat.getDateInstance();

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		JLabel cell = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		if(value instanceof IconizedInterface){
			return getIconicedInterfaceComponent((IconizedInterface) value, cell);
		} else if (value instanceof Date){
			return getCellComponent((Date) value, cell);
		}
		cell.setIcon(null);
		return cell;
	}

	private Component getCellComponent(Date value, JLabel cell) {
		cell.setIcon(null);
		cell.setText(dateFormat.format(value));
		return cell;
	}

	private JLabel getIconicedInterfaceComponent(IconizedInterface value, JLabel cell){
		cell.setIcon(value.getIcon());
		cell.setText(value.toString());
		return cell;
	}
}
